package com.vietnam.power.domain;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Lob;

import org.springframework.context.i18n.LocaleContextHolder;

import com.vietnam.power.persitence.ProductGroup;

public class Product {
	private Integer id;
	private boolean available;
	private Date createdDate;
	private String description;
	private String highlight;
	private String keyword;
	private String name;
	private int quantity;
	private String shortDescription;
	private boolean status;
	private BigDecimal unitPrice;
	private long totalPage;
	private List<Integer> productImages;
	private int productGroupId;
	private String productGroupName;
	private BigDecimal totalPrice;

	public Product() {
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public boolean getAvailable() {
		return this.available;
	}

	public void setAvailable(boolean available) {
		this.available = available;
	}

	public Date getCreatedDate() {
		return this.createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	@Lob
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getHighlight() {
		return this.highlight;
	}

	public void setHighlight(String highlight) {
		this.highlight = highlight;
	}

	public String getKeyword() {
		return this.keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getQuantity() {
		return this.quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public String getShortDescription() {
		return this.shortDescription;
	}

	public void setShortDescription(String shortDescription) {
		this.shortDescription = shortDescription;
	}

	public boolean getStatus() {
		return this.status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public BigDecimal getUnitPrice() {
		return this.unitPrice;
	}

	public void setUnitPrice(BigDecimal unitPrice) {
		this.unitPrice = unitPrice;
	}

	public String getUnitPriceAsSale() {
		NumberFormat nf = NumberFormat.getCurrencyInstance(LocaleContextHolder.getLocale());
		nf.setMaximumFractionDigits(0);
		return nf.format(this.unitPrice);
	}

	public long getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(long totalPage) {
		this.totalPage = totalPage;
	}

	public List<Integer> getProductImages() {
		if (productImages == null) {
			productImages = new ArrayList<Integer>();
		}
		return productImages;
	}

	public void setProductImages(List<Integer> productImages) {
		this.productImages = productImages;
	}

	public int getProductGroupId() {
		return productGroupId;
	}

	public void setProductGroupId(int productGroupId) {
		this.productGroupId = productGroupId;
	}

	public String getTotalPriceAsSale() {
		NumberFormat nf = NumberFormat.getCurrencyInstance(LocaleContextHolder.getLocale());
		nf.setMaximumFractionDigits(0);
		return nf.format(getTotalPrice());
	}

	public BigDecimal getTotalPrice() {
		this.totalPrice = unitPrice.multiply(new BigDecimal(quantity));
		return this.totalPrice;
	}

	public void setTotalPrice(BigDecimal totalPrice) {
		this.totalPrice = totalPrice;
	}

	public String getProductGroupName() {
		return productGroupName;
	}

	public void setProductGroupName(String productGroupName) {
		this.productGroupName = productGroupName;
	}

	public void convertToDomain(com.vietnam.power.persitence.Product p) {
		this.id = p.getId();
		this.name = p.getName();
		this.highlight = p.getHighlight();
		this.shortDescription = p.getShortDescription();
		this.description = p.getDescription();
		this.available = p.getAvailable();
		this.status = p.getStatus();
		this.keyword = p.getKeyword();
		this.quantity = p.getQuantity();
		this.unitPrice = p.getUnitPrice();
		this.productGroupId = p.getProductGroup().getId();
		this.productGroupName = p.getProductGroup().getName();
		for (com.vietnam.power.persitence.ProductImage pi : p.getProductImages()) {
			getProductImages().add(pi.getFileUpload().getId());
		}
	}

	public void convertToPersist(com.vietnam.power.persitence.Product p) {
		p.setName(this.name);
		p.setHighlight(this.highlight);
		p.setShortDescription(this.shortDescription);
		p.setDescription(this.description);
		p.setAvailable(this.available);
		p.setStatus(this.status);
		p.setKeyword(this.keyword);
		p.setQuantity(this.quantity);
		p.setUnitPrice(this.unitPrice);
		p.setProductGroup(new ProductGroup(this.productGroupId));
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Product) {
			if (this.id == ((Product) obj).getId()) {
				return true;
			}
		}
		return false;
	}

	@Override
	public int hashCode() {
		return this.id;
	}
}